package by.grsu.hdm.devices;

import java.util.Random;

import by.grsu.hdm.media.IMediaPlayer;
import by.grsu.hdm.media.IMediaPlayerSupport;
import by.grsu.hdm.media.MiniMediaPlayer;
import by.grsu.hdm.model.HomeDevice;
import by.grsu.hdm.model.INetworkAvailable;
import by.grsu.hdm.model.IUsbPort;
import by.grsu.hdm.model.IUsbSupport;
import by.grsu.hdm.model.UsbPort;

public class MediaCenter extends HomeDevice implements IUsbSupport, INetworkAvailable, IMediaPlayerSupport {
	
	private long trafficIn;
	private long trafficOut;
	private IMediaPlayer mediaPlayer;
	
	private UsbPort usbPort;
	
	public MediaCenter() {
		defaultConfiguration();
	}
	
	public MediaCenter(String id) {
		super(id);
		
		defaultConfiguration();
	}
	
	protected void defaultConfiguration()
	{
		this.trafficIn = 0;
		this.trafficOut = 0;
		
		this.usbPort = null;
		this.mediaPlayer = null;
	}
	
	@Override
	public long getOutgoingTraffic() {
		return this.trafficOut;
	}
	
	@Override
	public long getIncomingTraffic() {
		return this.trafficIn;
	}

	@Override
	protected void afterTurningOn() {
		this.trafficIn = Math.abs(new Random().nextInt());
		this.trafficOut = Math.abs(new Random().nextInt());
		System.out.println(getDeviceId() + " is turned on");
	}

	@Override
	protected void beforeTurningOff() {
		System.out.println(getDeviceId() + " is preparing for shutdown");
	}

	@Override
	public IUsbPort getUsbPort() {
		if (this.usbPort == null) { 
			this.usbPort = new UsbPort("USB-" + getDeviceId());
		}
		
		return this.usbPort;
	}
	
	public IMediaPlayer getMediaPlayer(){
		
		if(this.mediaPlayer == null){
			this.mediaPlayer = new MiniMediaPlayer();
		}
		
		return this.mediaPlayer;
	}
	
}
